/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActMyTask.java  2014年9月16日 下午5:03:57 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.ShareScoreBean;
import com.android.tiango.baba.entity.TaskEntity;
import com.android.tiango.baba.entity.TaskModel;
import com.android.tiango.baba.util.DensityUtil;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.MyTaskTopView;
import com.android.tiango.baba.view.TaskScrollView;
import com.android.tiango.baba.view.TaskTextView;

/**
 * 我的任务
 * @author givon
 * 
 */
public class ActMyTask extends BaBaseActivity {
	private TaskScrollView mScrollView;
	// private TaskTextView mTaskTextView;
	private MyTaskTopView mMyTaskTopView;
	private LinearLayout mLy_Container;
	private static ArrayList<TaskTextView> mArrayList = new ArrayList<TaskTextView>();
	static int[] location = new int[2];
	private int[] colorsr = { R.color.color_tag1, R.color.color_tag2, R.color.color_tag3,
			R.color.color_tag4, R.color.color_tag5, R.color.color_tag6, R.color.color_tag7,
			R.color.color_tag8 };
	private ArrayList<TaskModel> mTaskModels;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			for (int i = 0; i < mArrayList.size(); i++) {
				onMeasureTxt(mArrayList.get(i));
			}
			mScrollView.sendScroll(TaskScrollView.UP, 0);
		};
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.layout_mytask);
		mMyTaskTopView = (MyTaskTopView) findViewById(R.id.mt_view);
		mScrollView = (TaskScrollView) findViewById(R.id.task_scroll);
		mLy_Container = (LinearLayout) findViewById(R.id.container);
		initListener();
		laodMyTasks();
	}

	private void initView() {
		// for (int i = 0; i < 13; i++) {
		// mTaskModels.add(new TaskModel());
		// }
		mLy_Container.removeAllViews();
		mArrayList.clear();
		int floo = mTaskModels.size() / 2;
		int yy = mTaskModels.size() % 2;
		for (int i = 0; i <= floo + yy; i++) {
			LayoutParams lypLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LinearLayout layout = new LinearLayout(this);
			layout.setLayoutParams(lypLayoutParams);
			layout.setGravity(Gravity.CENTER_HORIZONTAL);
			for (int k = 0; k < 2; k++) {
				int index = i * 2 + k;
				if (index > mTaskModels.size() - 1) {
					return;
				}
				TaskModel model = mTaskModels.get(i);
				if(null==model){
					return;
				}
				TaskTextView taskTextView = new TaskTextView(this);
				taskTextView.setTaskModel(model);
				setTextColor(taskTextView);
				mScrollView.AddListener(taskTextView);
				LayoutParams params = new LayoutParams(DensityUtil.dip2px(this, 150),
						DensityUtil.dip2px(this, 150));
				params.gravity = Gravity.CENTER;
				// params.weight =1;
				if (k == 0) {
					params.setMargins(15, 10, 5, 10);
				} else if (k == 1) {
					params.setMargins(5, 10, 15, 10);
				}
				layout.addView(taskTextView, params);
				mArrayList.add(taskTextView);
			}
			mLy_Container.addView(layout);
		}
		mHandler.sendEmptyMessageDelayed(0, 1000);
	}

	@Override
	protected void onStart() {
		super.onStart();

	}

	private void setTextColor(TaskTextView view) {
		int color;
		int r = (int) (Math.random() * colorsr.length);
		view.setBarColor(getResources().getColor(colorsr[r]));
	}

	private void initListener() {
		for (int i = 0; i < mArrayList.size(); i++) {
			mScrollView.AddListener(mArrayList.get(i));
		}
		mScrollView.setScrollTopListener(mMyTaskTopView);
		mScrollView.setMyTaskTopView(mMyTaskTopView);
		ViewTreeObserver vto = mScrollView.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			public void onGlobalLayout() {
				mMyTaskTopView.setmTaskScrollView(mScrollView);
			}
		});
	}

	private static void onMeasureTxt(TaskTextView view) {
		// view.getLocationInWindow(location);
		// view.setLocHeight(location[1]);
	}

	/**
	 * 我的任务
	 */
	private void laodMyTasks() {
		// User/MyTasks
		if (!ShareCookie.isLoginAuth()) {
			showActivity(ActLogin.class, false);
			return;
		}
		HttpClientAsync clientAsync = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());
		clientAsync.get(HttpUrl.getUrl(HttpUrl.MYTASKS), params, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				dismissWaitingDialog();
				TaskEntity entity = (TaskEntity) arg0;
				if (null != entity) {
					if (null != entity.getData() && entity.getData().size() > 0) {
						mTaskModels = entity.getData();
						initView();
					} else {
						ToastUtils.showMessage(getStringValue(R.string.s_notmsg));
					}
				}
			}

			@Override
			public void onHttpStarted() {
				showWaitingDialog();
			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				dismissWaitingDialog();
				if (StringUtils.isEmpty(arg1)) {
					arg1 = getStringValue(R.string.info_net_error);
				}
				ToastUtils.showMessage(arg1);
			}
		}, TaskEntity.class);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		mScrollView = null;
		finish();
	}

}
